package com.aiwhose.aiwhosefile.controller;


import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.stp.SaTokenInfo;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.map.MapUtil;
import com.aiwhose.aiwhosecore.bean.Result;
import com.aiwhose.aiwhosecore.bean.file.File;
import com.aiwhose.aiwhosefile.bean.FileQiniuSecurity;
import com.aiwhose.aiwhosefile.service.FileService;
import com.aiwhose.aiwhosefile.utils.QiniuYunUtils;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@CrossOrigin
@SaCheckLogin
@RestController
@RequestMapping("/qiniuyunweb")
@Slf4j
public class QiniuyunWebController {
    @Resource
    FileService fileService;
    @Value("${qiniuyun.accessKey}")
    private String accessKey;
    @Value("${qiniuyun.secretKey}")
    private String secretKey;
    @Value("${qiniuyun.public.pu_bucket}")
    private String pu_bucket;
    @Value("${qiniuyun.public.pu_domainOfBucket}")
    private String pu_domainOfBucket;
    @Value("${qiniuyun.private.pr_bucket}")
    private String pr_bucket;
    @Value("${qiniuyun.private.pr_domainOfBucket}")
    private String pr_domainOfBucket;

    @GetMapping("/getUploadToken")
    @ResponseBody
    public Result getUploadToken(@RequestParam String fileName) {
        SaTokenInfo tokenInfo = StpUtil.getTokenInfo();
        String loginId = (String) tokenInfo.loginId;
        String path = "Aiwhose/" +loginId+ QiniuYunUtils.createPath(fileName);


        FileQiniuSecurity fileQiniuSecurity = getFileQiniuSecurity();
        String token = fileService.getToken(fileQiniuSecurity, path);
        HashMap<String, String> map = new HashMap<>();
        map.put("token", token);
        map.put("path", path);
        map.put("url", fileQiniuSecurity.getBucketUrl() + "/");
        map.put("region", fileQiniuSecurity.getBucketRegion());
        return Result.ok(map);
    }


    @PostMapping("/uploadToData")
    @ResponseBody
    public Result uploadToData(@RequestBody Map<String, Object> body) throws Exception {
        File file = MapUtil.get(body, "file", File.class);
        String fileQiniuSecurityid = MapUtil.get(body, "fileQiniuSecurity", String.class);
        FileQiniuSecurity fileQiniuSecurity = getFileQiniuSecurity();
        SaTokenInfo tokenInfo = StpUtil.getTokenInfo();
        String loginId = (String) tokenInfo.loginId;
        file.setUser(loginId);
        file.setAccessAuthority(fileQiniuSecurity.getType());
        boolean save = fileService.save(file);
        return Result.ok(save);
    }


    private FileQiniuSecurity getFileQiniuSecurity() {
        FileQiniuSecurity fileQiniuSecurity = new FileQiniuSecurity();
        fileQiniuSecurity.setSecretKey(secretKey);
        fileQiniuSecurity.setAccessKey(accessKey);
        fileQiniuSecurity.setBucketName(pu_bucket);
        fileQiniuSecurity.setBucketUrl(pu_domainOfBucket);

        return fileQiniuSecurity;
    }
}
